\subsubsection{Phase3: Separate Code}
\paragraph{Purpose}
This phase will move the intended code that will be translated into kernels that will run on the device to a separate kernel file.

GPU programs have code that runs on both the host machine and the GPU device.  At this point it is time to separate our code.  This is a little more complicated than putting computation code into a kernel file.  We also need to separate variables.  This means allocating memory, and determining where the data needs to be moved between the device and host memory.   Also, one last change is made to the preprocessor directives, we add includes for the new kernel file.

    There are 3 parts to separating the code.  Separate the variables, the computation.
Separating the computation is somewhat easy.  We create a new file, lets call it \'mykernels.cu\'  We add '\#include "mykernels.cu" ' to the end of the preprocessor directives.  Then we move the sections of device code to this file.  As we move the sections of device code over, we leave markers.  One marker goes into host code the other goes at the head of each section of device code.  In the case of function calls, all function calls of the same type are replaced by the same marker.
    Separating the variables is a little more involved.  Host variables are left alone, although they can be clarified as host variables by adding an 'h\_' prefix.  For device variables, they should be augmented by a 'd\_' prefix and the memory allocation is changed to the 'cudaMalloc\(\)' function.  The more complicated variables are the Mixed variables.  For these there has to be a copy on the host as well as a copy on the device.  So, there will be two versions of the variable, with the same conventions as described above with the appropriate prefix.  Then cudaMemCopy() calls are inserted.  For input variables, they are inserted after initialization or input is given.  For output variables, they are inserted before any output is used.  

\paragraph {Output}
Output will be 2 files.  The first file is the main code that will run on the host.  The extension will be 'cu'.  The other file will contain the device code.  The name of the file will be 'myproject\_kernel.cu'.  

The main file will have the following changes ...

The Kernel file will contain ...



\paragraph{Meta Algoritm}

\begin{description}
\item [Step 1]

\item [Step 2] 

\item [Step 3] 

\item [Step 4]

\end{description}




\paragraph{Sample Code}


\input{tabs/tab-algorithm_phase3}



\begin{cudablock}
///// Preproc Begin
#include <stdio>  /// Keep
#include <mpi.h> /// Change
///// Preproc End

\end{cudablock} 



\begin{cudablock}

///// Preproc Begin
#include <stdio>  /// Keep
#include <mpi.h> /// Change
#include "main_kernel.cu" /// kernel
///// Preproc End

/////Declarations 

/////Computation

\end{cudablock} 

